/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.command;

import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.JInternalFrame;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.controller.Controller;
import br.ufmg.dees.insane.commons.dialogs.TabbedDialog;
import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;
import br.ufmg.dees.insane.ui.rich.full.dialogs.ElmIniEqNodalForcesDialog;

/**
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since April 2004
 */
public class SetElmIniEqNodalForcesCommand implements Command, Cloneable {
	
	private InsaneInternalInterface parent;
	private Controller controller;
	private DrawingArea da;
	
//*****************************************************************************
	
	/** Creates new SetElmIniEqNodalForcesCommand */
	public SetElmIniEqNodalForcesCommand(JInternalFrame parent) {
		this.parent = (InsaneInternalInterface)parent;
		controller = this.parent.getController();
		da = this.parent.getDrawing();
	}
	
//*****************************************************************************
	
	/** Execute method. */
	public void execute() {
		
		boolean changed = false;
		
		ArrayList elms = controller.getSelection();
		if ( elms.size() < 1 ) {
			controller.selectAll();
			elms = controller.getSelection();
		}
		
		if (elms.size() == 0) {
			parent.addOutputText("There is no elements in model.");
			return;
		}
		
        ElmIniEqNodalForcesDialog dlg = new ElmIniEqNodalForcesDialog(parent);
        int retVal = dlg.showDialog();
        if (retVal == TabbedDialog.OK_OPTION){
			
			ListIterator elements = elms.listIterator();
			while(elements.hasNext()) {
				FrameElement elm = (FrameElement)elements.next();
				
				if (elm.getInitialNodalForce(0) != dlg.eqFx1)
					changed = true;
				if (elm.getInitialNodalForce(1) != dlg.eqFy1)
					changed = true;
				if (elm.getInitialNodalForce(2) != dlg.eqFz1)
					changed = true;
				if (elm.getInitialNodalForce(3) != dlg.eqMx1)
					changed = true;
				if (elm.getInitialNodalForce(4) != dlg.eqMy1)
					changed = true;
				if (elm.getInitialNodalForce(5) != dlg.eqMz1)
					changed = true;
				if (elm.getInitialNodalForce(6) != dlg.eqFx2)
					changed = true;
				if (elm.getInitialNodalForce(7) != dlg.eqFy2)
					changed = true;
				if (elm.getInitialNodalForce(8) != dlg.eqFz2)
					changed = true;
				if (elm.getInitialNodalForce(9) != dlg.eqMx2)
					changed = true;
				if (elm.getInitialNodalForce(10) != dlg.eqMy2)
					changed = true;
				if (elm.getInitialNodalForce(11) != dlg.eqMz2)
					changed = true;
				
				//Setting InitialEquivalentNodalForce
				elm.setInitialNodalForce(0, dlg.eqFx1);
				elm.setInitialNodalForce(1, dlg.eqFy1);
				elm.setInitialNodalForce(2, dlg.eqFz1);
				elm.setInitialNodalForce(3, dlg.eqMx1);
				elm.setInitialNodalForce(4, dlg.eqMy1);
				elm.setInitialNodalForce(5, dlg.eqMz1);
				elm.setInitialNodalForce(6, dlg.eqFx2);
				elm.setInitialNodalForce(7, dlg.eqFy2);
				elm.setInitialNodalForce(8, dlg.eqFz2);
				elm.setInitialNodalForce(9, dlg.eqMx2);
				elm.setInitialNodalForce(10, dlg.eqMy2);
				elm.setInitialNodalForce(11, dlg.eqMz2);
				
			}
			
		}
		
		//parent.addToUndo((Command)this.clone());
		controller.unSelectAll();
		controller.compose();
		da.repaint();
		
		if (changed)
			parent.cleanResults();
		
	}
	
//*****************************************************************************
	
	/** Impossible to undo - not implemented. */
	public void undo() {
	}
	
//*****************************************************************************
	
	/** Impossible to redo - not implemented. */
	public void redo() {
	}
	
//*****************************************************************************
	
	/** Clone method. */
	public Object clone() {
		try {
			SetElmIniEqNodalForcesCommand cmd = (SetElmIniEqNodalForcesCommand)super.clone();
			return cmd;
		}catch (CloneNotSupportedException e) {
			throw new Error("Clonning ERROR - This should never happen!");
		}
	}
	
//*****************************************************************************
}

